package com.ssy.lingxi.platform.manage.handler.notify;

import cn.hutool.json.JSONUtil;
import com.rabbitmq.client.Channel;
import com.ssy.lingxi.component.rabbitMQ.constants.MessageConstants;
import com.ssy.lingxi.platform.manage.api.model.dto.area.AreaAddDTO;
import com.ssy.lingxi.platform.manage.service.IAreaService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 同步行政区域信息-消息队列监听类
 * @author jw.chen
 * @version 2.0.0
 * @date 2021/10/14
 */
@Component
@RabbitListener(queues = MessageConstants.OPEN_API_AREA_QUEUE)
public class AreaMessageHandler {

    @Resource
    private IAreaService areaService;

    private final Logger log = LoggerFactory.getLogger(this.getClass());

    @RabbitHandler
    public void handler(Channel channel, Message message, String json){
        try{
            //转换对象
            AreaAddDTO areaRequest = JSONUtil.toBean(json, AreaAddDTO.class);

            //新增/修改行政区域
            areaService.saveOrUpdateArea(areaRequest);
        }catch (Exception e){
            //记录异常日志
            log.error(e.getMessage());
        }

        try{
            //手动确认
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
